
package com.common.allCommon;

import com.product.android.business.ApplicationVariable;
import com.product.android.business.login.BindUser;
import com.product.android.commonInterface.contact.RankFilterInfo.Gender;

public class PhotoScreenCondition {

	public static PhotoScreenCondition instance;
	private int m_iSavedOrder = 0;			//花最多的 or 随机
	private int m_iNewOrder = 0;	
	private boolean m_bOrderChanged = false;
	private int saveGender = Gender.ALL;
	private int newGender = Gender.ALL;
	private boolean genderChange = false;	
	private boolean unitConditionChanage = false;
	private PhotoScreenStatus saveStatus;
	private PhotoScreenStatus newStatus;
	private boolean reset = false;

	public void reInitValue(){
		m_iSavedOrder = 0;
		m_iNewOrder = 0;
		m_bOrderChanged=false;
		saveGender = Gender.ALL;
		newGender = Gender.ALL;		
		genderChange = false;
		
		unitConditionChanage = false;
		saveStatus = null;
		newStatus = null;
		reset = true;
	}
	
	public void initNewValue(){
		//点击重置按钮时调用
		m_iNewOrder = 0;
		newGender = -1;		
		newStatus = new PhotoScreenStatus(StarappApplicationVariable.INSTANCE.getCurrentUser().getUnitid(),
									0, StarappApplicationVariable.INSTANCE.getCurrentUser().getUnitname());
	}
	
	public void init() {
		if(StarappApplicationVariable.INSTANCE.getCurrentUser()!=null){
			if(saveStatus!=null){
				saveStatus.unitid =  StarappApplicationVariable.INSTANCE.getCurrentUser().getUnitid();
				saveStatus.deptid = 0;
				saveStatus.selectedNodeName = null;
			}
		}
		m_iSavedOrder = 0;
		saveGender = Gender.ALL;
	}
	
	public void copyNewValue(){
		//初始化筛选对话框时调用
		m_iNewOrder = m_iSavedOrder;
		m_bOrderChanged=false;
		newGender = saveGender;
		genderChange = true;		
		newStatus = saveStatus;
		unitConditionChanage = false;
	}
	
	public boolean isDefaultCondition() {
		return !(m_iSavedOrder != 0 || saveGender != Gender.ALL || (saveStatus != null && saveStatus.deptid != 0));
	}

	private PhotoScreenCondition(){
		if(StarappApplicationVariable.INSTANCE.getCurrentUser()!=null){
			saveStatus = new PhotoScreenStatus(StarappApplicationVariable.INSTANCE.getCurrentUser().getUnitid(),0,null);
		}
	}
	
	public static PhotoScreenCondition getInstance(){
		if(instance == null){
			instance = new PhotoScreenCondition ();
		}
		return instance;
	}
	
	public String getSelectedNodeName() {
		if(saveStatus!=null&&saveStatus.selectedNodeName!=null){
			return saveStatus.selectedNodeName;
		}
		else{
			return StarappApplicationVariable.INSTANCE.getCurrentUser().getUnitname();
		}
	}
	
	public void setOrderChanged(boolean bOrderChanged) {
		this.m_bOrderChanged = bOrderChanged;
	}
	
	public void setGenderChange(boolean genderChange) {
		this.genderChange = genderChange;
	}
	
	public void setUnitConditionChanage(boolean unitConditionChanage) {
		this.unitConditionChanage = unitConditionChanage;
	}
	
	public boolean isOrderChange() {
		return m_bOrderChanged;
	}
	
	public boolean isGenderChange() {
		return genderChange;
	}
	
	public boolean isUnitConditionChanage() {
		return unitConditionChanage;
	}
	
	public boolean isAnyConditionChanged(){
		return m_bOrderChanged || genderChange || unitConditionChanage;
	}
	
	public int getOrder() {
		return m_iSavedOrder;
	}
	
	public int getGender() {
		return saveGender;
	}

	public int getUnitid() {
		if(saveStatus!=null)
		return saveStatus.unitid;
		else {
			BindUser user = StarappApplicationVariable.INSTANCE.getCurrentUser();
			if(user != null){
				return user.getUnitid();
			}
			else{
				return 0;
			}
		}
	}
	
	public int getDeptid() {
		if(saveStatus!=null)
		return saveStatus.deptid;
		else return 0;
	}
	
	public void setNewOrder(int iNewOrder) {
		this.m_iNewOrder = iNewOrder;
	}
	
	public void setNewGender(int newGender) {
		this.newGender = newGender;
	}

	public void setUnitParam(int unitid,int deptid,String selectedNodeName ){
		newStatus = new PhotoScreenStatus(unitid,deptid,selectedNodeName);
	}

	public void setNewStatus(PhotoScreenStatus newStatus) {
		this.newStatus = newStatus;
	}
	
	public PhotoScreenStatus getNewStatus() {
		return newStatus;
	}
	
	public int getNewStatusUnitid() {
		if(newStatus!=null){
			return newStatus.unitid;
		}
		return 0;
	}
	
	public int getNewStatusDeitid(){
		if(newStatus!=null){
			return newStatus.deptid;
		}
		return 0;
	}

	public int getNewOrder() {
		return m_iNewOrder;
	}
	
	public int getNewGender() {
		return newGender;
	}

	public CharSequence getNewStatusSelectedNodeName() {
		if(newStatus!=null){
			return newStatus.selectedNodeName;
		}
		return "";
	}
	
	public void validateionChanage() {
		if (m_iNewOrder != m_iSavedOrder) {
			m_iSavedOrder = m_iNewOrder;
			m_bOrderChanged = true;
		}
		if (newGender != saveGender) {
			saveGender = newGender;
			genderChange = true;
		}
		if (saveStatus != null
				&& newStatus != null
				&& (saveStatus.deptid != newStatus.deptid || saveStatus.unitid != newStatus.unitid)) {
			saveStatus = newStatus;
			unitConditionChanage = true;
		} else if (saveStatus == null && newStatus != null) {
			saveStatus = newStatus;
			unitConditionChanage = true;
		}
	}

	public boolean isReset() {
		return reset;
	}
	
	public void setReset(boolean reset){
		this.reset = reset;
	}
}

class PhotoScreenStatus{
	public int unitid;
	public int deptid;
	String selectedNodeName;
	
	public int getUnitid(){
		return unitid;
	}
	
	public int getDeptid(){
		return deptid;
	}
	
	public PhotoScreenStatus(int unitid,int deptid,String selectedNodeName){
		this.unitid = unitid;
		this.deptid = deptid;
		this.selectedNodeName = selectedNodeName;
	}
}